package com.zyf.project.kvdb.wal.config;

import lombok.Data;
import lombok.ToString;

/**
 * <h1>WAL 配置参数</h1>
 *
 * @author Zhou Yifan
 */
@Data
@ToString
public class WalConfig {

    /* 默认配置数据，和下面的配置属性相对应 */

    private static final int DEFAULT_FLUSH_INTERVAL = 1;

    private static final boolean DEFAULT_FLUSH_METADATA = false;

    private static final boolean DEFAULT_TRUNCATE_EXISTING = false;

    /**
     * 旧日志清理阈值，默认超过 100M 清理
     */
    private static final long DEFAULT_MAX_CHECKPOINT_SIZE = 104857600;

    /* 配置属性 */

    /**
     * 日志文件，这里是绝对路径
     */
    private String logFile;

    /**
     * 是否清空已存在文件内容
     */
    private boolean truncateExisting;

    /**
     * 刷新时间配置
     * <table>
     *     <tr>
     *         <td align="center"><b>-1</b></td>
     *         <td>跟随系统</td>
     *     </tr>
     *     <tr>
     *         <td align="center"><b>0</b></td>
     *         <td>实时同步</td>
     *     </tr>
     *     <tr>
     *         <td align="center"><b>n</b></td>
     *         <td>间隔 n 秒刷盘 (n>0)</td>
     *     </tr>
     * </table>
     */
    private int flushInterval;

    /**
     * 是否刷新元信息
     */
    private boolean flushMetaData;

    /**
     * 快照超过多大时清理
     */
    private long maxCheckpointSize;

    /* 构造函数 */

    public WalConfig(String logFile) {
        this(logFile, DEFAULT_FLUSH_INTERVAL);
    }

    public WalConfig(String logFile, int flushInterval) {
        this(logFile, flushInterval, DEFAULT_FLUSH_METADATA);
    }

    public WalConfig(String logFile, int flushInterval, boolean flushMetaData) {
        this(logFile, flushInterval, flushMetaData, DEFAULT_TRUNCATE_EXISTING);
    }

    public WalConfig(String logFile, int flushInterval, boolean flushMetaData, boolean truncateExisting) {
        this(logFile, flushInterval, flushMetaData, truncateExisting, DEFAULT_MAX_CHECKPOINT_SIZE);
    }

    public WalConfig(String logFile, int flushInterval, boolean flushMetaData, boolean truncateExisting, long maxCheckpointSize) {
        this.logFile = logFile;
        this.flushInterval = flushInterval;
        this.flushMetaData = flushMetaData;
        this.truncateExisting = truncateExisting;
        this.maxCheckpointSize = maxCheckpointSize;
    }
}
